// pages/logs/logs.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 单词
    content: null,
    // 音标
    pron: null,
    // 词性
    definition: null,
    // 读音src
    audioURL: null,
    // 记录单词的具体信息
    List: {
      id: '',
      content: '',
      pron: '',
      definition: '',
      audioURL: '',
      showNot: '',
      collected: ''
    },
    // 计数器
    count: 0,
    url: null,
    // 单词的翻译是否显示
    showNot: false,
    // 单词已收藏
    collected: true,
    // 记录当前点击的单词
    click_word: null,
    //收藏夹
    collection: [],
    //
    show: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    //wx.removeStorageSync("collection")
    var collection = wx.getStorageSync("collection")
    if (collection === ''||collection.length==0) {
      collection = []
      that.setData({
        show: false
      })
    }
    var array = wx.getStorageSync("collection")
    this.setData({
      array: array,
      collection: collection
    })

    // 将收藏的单词的具体信息记录到数组中
    for (var i in array) {
      that.getaudiourl();
    }

  },
  // 获取单词的具体信息同时存入记录的数组List
  getaudiourl: function (e) {
    let that = this;
    var List = that.data.List
    var id = wx.getStorageSync(this.data.collection.length);
    var count = that.data.count
    var record = 'List[' + count + ']'
    // 判断是否为收藏的最后一个单词
    if (count != id) {
      var word = that.data.array[count]
      console.log("单词为" + word)
      wx.request({
        url: 'https://api.shanbay.com/bdc/search/?word=' + word,
        data: {},
        method: 'GET',
        success: function (res) {
          //console.log(123)
          //console.log(res)
          that.setData({
            content: res.data.data.content,
            audioURL: res.data.data.audio,
            pron: '/' + res.data.data.pronunciation + '/',
            definition: res.data.data.definition,
            [record]: {
              word: res.data.data.content,
              pron: '/' + res.data.data.pronunciation + '/',
              definition: res.data.data.definition,
              audioURL: res.data.data.audio,
              showNot: true,
              collected: true,
              id: count * 1 - 1,
            }
          })
        }
      })
      count++;
      that.setData({
        count: count
      })
    }
  },


  // 单词发声
  read: function (e) {
    var audiourl = e.currentTarget.id;
    console.log("id=" + audiourl)
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = false;
    innerAudioContext.src = audiourl;
    innerAudioContext.play()
    innerAudioContext.onPlay(() => { })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
    var definition = e.currentTarget.id;
    this.setData({
      click_word: definition
    })
  },


  show: function (e) {
    var id = e.currentTarget.id;
    var word = this.data.List
    var a = !this.data.List[id].showNot
    var chang = 'List[' + id + '].showNot'
    this.setData({
      [chang]: a
    })

  },

  collect: function (e) {
    var id = e.currentTarget.id;
    var word = this.data.List
    var a = this.data.List[id].collected
    var b = !this.data.List[id].collected
    if (a == false && b == true) {
      this.data.collection.push(this.data.array[id])
      console.log();
      wx.setStorageSync("collection", this.data.collection)
      console.log("收藏成功" + this.data.array[id]);
    } else {
      for (var i = 0; i < this.data.collection.length; i++) {
        if (this.data.collection[i] == this.data.array[id]) {
          this.data.collection.splice(i, 1)
          wx.setStorageSync("collection", this.data.collection);
          console.log("取消收藏" + this.data.array[id]);
          break;
        }
      }
    }
    var chang = 'List[' + id + '].collected'
    this.setData({
      [chang]: b
    })
  },


  // 将缓存中的用户数据记录到数据库
  getData: function () {
    const db = wx.cloud.database();
    var collection = this.data.collection
    db.collection('userData').doc(this.data.countID).update({
      data: {
        collection: collection,
      },
    })
  },

  getmessage: function () {
    // 获取此次用户的openid
    var _openid = wx.getStorageSync("_openid");
    const db = wx.cloud.database();
    // 获取数据库中用户的信息
    const cont = db.collection('usermessage');
    cont.where({
      _openid: _openid,
    }).get({
      success: res => {
        var username = res.data[0].username;
        this.setData({
          username: username,
        })
      }
    })
    // 获取数据库中用户的应用数据
    db.collection('userData').where({
      _openid: _openid,
    }).get({
      success: res => {
        var _id = res.data[0]._id;
        this.setData({
          countID: _id,
        })
      }
    })

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  this.getmessage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   this.getData()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})